/*
 * pps_bom
 */
package com.pitop.bom.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.bom.entity.Bom;
import com.pitop.bom.service.BomSV;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * bom 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/bom")
@Slf4j
@Api(value = "bom控制器", description = "bom控制器")
public class BomCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private BomSV bomSV;

    /**
     * 查询Bom一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public Bom load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Bom bom = bomSV.load(id, code);
        log.info(JSON.toJSONString(bom));
        return bom;
    }

    /**
     * 查询Bom一个详情信息，(将查询关联数据)
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public Bom get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Bom bom = bomSV.get(id, code);
        log.info(JSON.toJSONString(bom));
        return bom;
    }

    /**
     * 根据条件id查询Bom一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public Bom loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Bom bom = bomSV.loadById(id);
        log.info(JSON.toJSONString(bom));
        return bom;
    }

    /**
     * 根据条件code查询Bom一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public Bom loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Bom bom = bomSV.loadByCode(code);
        log.info(JSON.toJSONString(bom));
        return bom;
    }

    /**
     * 根据条件name查询Bom一个详情信息
     *
     * @param name bom名称
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "bom名称", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/name/{name}")
    @ResponseBody
    public Bom loadByName(@PathVariable String name) {
        if (StringTools.isEmpty(name)) {
            return null;
        }
        Bom bom = bomSV.loadByName(name);
        log.info(JSON.toJSONString(bom));
        return bom;
    }

    /**
     * 根据条件bomCode查询Bom一个详情信息
     *
     * @param bomCode bom名称
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomCode", value = "bom名称", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/bomCode/{bomCode}")
    @ResponseBody
    public Bom loadByBomCode(@PathVariable String bomCode) {
        if (StringTools.isEmpty(bomCode)) {
            return null;
        }
        Bom bom = bomSV.loadByBomCode(bomCode);
        log.info(JSON.toJSONString(bom));
        return bom;
    }

    /**
     * 根据条件id查询Bom一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public Bom getById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Bom bom = bomSV.getById(id);
        log.info(JSON.toJSONString(bom));
        return bom;
    }

    /**
     * 根据条件code查询Bom一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public Bom getByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Bom bom = bomSV.getByCode(code);
        log.info(JSON.toJSONString(bom));
        return bom;
    }

    /**
     * 查询Bom信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Bom信息集合", notes = "查询Bom信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query"),
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<Bom> list(@RequestBody @ApiIgnore Bom bom, Integer curPage, Integer pageSize) {
        Page<Bom> page = new Page<Bom>(pageSize, curPage);
        List<Bom> boms = bomSV.list(bom, page.genRowStart(), page.getPageSize());
        int total = bomSV.count(bom);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return boms;
    }

    /**
     * 查询Bom信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Bom信息集合", notes = "查询Bom信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<Bom> listByPk(Long id, String code, Integer curPage, Integer pageSize) {
        Page<Bom> page = new Page<Bom>(pageSize, curPage);
        List<Bom> boms = bomSV.list(id, code, page.genRowStart(), page.getPageSize());
        int total = bomSV.count(id, code);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return boms;
    }

    /**
     * 统计Bom信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计Bom信息数量根据主键", notes = " 统计Bom信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id, String code) {
        return bomSV.count(id, code);
    }

    /**
     * 统计Bom信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Bom信息数量", notes = "统计Bom信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "子级级别", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom代码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态[开发Develop，稳定Stable，停用 Stop]", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody Bom bom) {
        return bomSV.count(bom);
    }

    /**
     * 创建Bom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Bom", notes = "创建Bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom代码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query"),
            @ApiImplicitParam(name = "account", value = "操作人账户", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "操作人编码", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public Bom build(@RequestBody @ApiIgnore Bom bom, String account, String code) {
        bomSV.save(bom, account, code);
        return bom;
    }

    /**
     * 修改Bom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Bom", notes = "修改Bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "子级级别", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom代码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query"),
            @ApiImplicitParam(name = "account", value = "账户信息", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "账户编码", paramType = "query"),
    })
    @PutMapping("/modify")
    @ResponseBody
    public Bom modify(@RequestBody @ApiIgnore Bom bom, String account, String code) {
        bomSV.modify(bom, account, code);
        return bom;
    }

    /**
     * 删除Bom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Bom", notes = "删除Bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        bomSV.delete(id, code);
        return BeanRet.create(true, "删除Bom成功");
    }

    /**
     * 修改组件状态
     *
     * @param code  组件编码
     * @param state 状态
     * @return
     */
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "状态(1032):Develop(开发) Stable(稳定) Stop(停用)", paramType = "query", required = true)
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, String state) {
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(state)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        bomSV.updateState(code, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

    /**
     * 复制bom
     *
     * @param code bom编码
     * @return
     */
    @ApiOperation(value = "复制bom", notes = "复制bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "bom编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "account", value = "账户", paramType = "query", required = true),
            @ApiImplicitParam(name = "accountCode", value = "账户编码", paramType = "query", required = true),
    })
    @GetMapping("/copy")
    @ResponseBody
    public BeanRet copy(String code, String account, String accountCode) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        bomSV.copy(code, account, accountCode);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

    /**
     * 查询未绑定成品的bom
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询未绑定成品的bom", notes = "查询未绑定成品的bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "bom名稱", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/queryUnBindProductBomList")
    @ResponseBody
    public List<Bom> queryUnBindProductBomList(String productCode, String name, String bomCode, Integer curPage, Integer pageSize) {
        Page<Bom> page = new Page<>(pageSize, curPage);
        List<Bom> boms = bomSV.queryUnBindProductBomList(productCode, name, bomCode, page.genRowStart(), page.getPageSize());
        log.info(JSON.toJSONString(page));
        return boms;
    }

    /**
     * 统计未绑定成品的bom数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计未绑定成品的bom数量", notes = "统计未绑定成品的bom数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "bom名稱", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/countUnBindProductBomList")
    @ResponseBody
    public Integer countUnBindProductBomList(String productCode, String name, String bomCode) {
        return bomSV.countUnBindProductBomList(productCode, name,bomCode );
    }

    /**
     * 已绑定成品的bom
     *
     * @return 分页对象
     */
    @ApiOperation(value = "已绑定成品的bom", notes = "已绑定成品的bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/queryBindProductBomList")
    @ResponseBody
    public List<Bom> queryBindProductBomList(String productCode, String name, String bomCode, Integer curPage, Integer pageSize) {
        Page<Bom> page = new Page<>(pageSize, curPage);
        List<Bom> boms = bomSV.queryBindProductBomList(productCode, name, bomCode, page.genRowStart(), page.getPageSize());
        log.info(JSON.toJSONString(page));
        return boms;
    }

    /**
     * 统计已绑定成品的bom数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计已绑定成品的bom数量", notes = "统计已绑定成品的bom数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/countBindProductBomList")
    @ResponseBody
    public Integer countBindProductBomList(String projectCode, String name, String bomCode) {
        return bomSV.countBindProductBomList(projectCode, name, bomCode);
    }


    /**
     * 查询未绑定项目的bom
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询未绑定成品的bom", notes = "查询未绑定成品的bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/queryUnBindProjectBomList")
    @ResponseBody
    public List<Bom> queryUnBindProjectBomList(String projectCode, String name, String bomCode, Integer curPage, Integer pageSize) {
        Page<Bom> page = new Page<>(pageSize, curPage);
        List<Bom> boms = bomSV.queryUnBindProjectBomList(projectCode, name, bomCode, page.genRowStart(), page.getPageSize());
        log.info(JSON.toJSONString(page));
        return boms;
    }

    /**
     * 统计未绑定项目的bom的数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计未绑定项目的bom的数量", notes = "统计未绑定项目的bom的数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/countUnBindProjectBomList")
    @ResponseBody
    public Integer countUnBindProjectBomList(String projectCode, String name, String bomCode) {
        return bomSV.countUnBindProjectBomList(projectCode, name, bomCode);
    }

    /**
     * 查询已绑定项目的bom
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询已绑定项目的bom", notes = "查询已绑定项目的bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/queryBindProjectBomList")
    @ResponseBody
    public List<Bom> queryBindProjectBomList(String projectCode, String name, String bomCode, Integer curPage, Integer pageSize) {
        Page<Bom> page = new Page<>(pageSize, curPage);
        List<Bom> boms = bomSV.queryBindProjectBomList(projectCode, name, bomCode, page.genRowStart(), page.getPageSize());
        log.info(JSON.toJSONString(page));
        return boms;
    }

    /**
     * 统计已绑定项目的bom的数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计已绑定项目的bom的数量", notes = "统计已绑定项目的bom的数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/countBindProjectBomList")
    @ResponseBody
    public Integer countBindProjectBomList(String projectCode, String name, String bomCode) {
        return bomSV.countBindProjectBomList(projectCode, name, bomCode);
    }
}
